package cn.itcast.erp.controller;

import cn.itcast.erp.annotation.CheckPower;
import cn.itcast.erp.entity.Emp;
import cn.itcast.erp.entity.Orderdetail;
import cn.itcast.erp.entity.Orders;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.service.OrderDetailService;
import cn.itcast.erp.service.OrdersService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import java.util.List;

/**
 * 采购Controller类
 *
 * @author 崔凯铭，罗宇航
 * @date 2019/6/12
 */

@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    @Resource
    private OrderDetailService orderDetailService;

    @CheckPower("采购申请")
    @RequestMapping("/listByPage")
    public AjaxResult listByPage(Orders orders) {
        return ordersService.findOrders(orders);
    }

    @CheckPower({"采购申请", "销售订单录入"})
    @RequestMapping("/insert")
    public AjaxResult insertOrders(@RequestBody Orders orders, @SessionAttribute("emp") Emp emp) {
        AjaxResult ar = new AjaxResult();
        int i = this.ordersService.insertOrders(orders, emp);
        if (i > 0) {
            ar.setMsg("添加成功");
            return ar;
        }
        ar.setMsg("添加失败");
        return ar;
    }

    @CheckPower("采购申请")
    @RequestMapping("/loadDetail")
    public List<Orderdetail> findOrderDetail(Orders orders) {
        return this.orderDetailService.findOrderDetail(orders);
    }

    @CheckPower("采购申请")
    @RequestMapping("/find")
    public Orders findOrderByUuid(Orders orders) {
        return this.ordersService.findOrderByUuid(orders);
    }

    @CheckPower({"采购申请", "采购订单审核"})
    @RequestMapping("/check")
    public AjaxResult check(Orders orders, @SessionAttribute("emp") Emp emp) {
        AjaxResult ar = new AjaxResult();
        int i = this.ordersService.checkOrder(orders, emp);
        if (i > 0) {
            ar.setMsg("审核成功");
            return ar;
        }
        ar.setMsg("审核失败");
        return ar;
    }

    @CheckPower({"采购申请", "采购订单确认"})
    @RequestMapping("/start")
    public AjaxResult start(Orders orders, @SessionAttribute("emp") Emp emp) {
        AjaxResult ar = new AjaxResult();
        int i = this.ordersService.startOrder(orders, emp);
        if (i > 0) {
            ar.setMsg("确认成功");
            return ar;
        }
        ar.setMsg("确认失败");
        return ar;
    }

    @CheckPower({"采购申请", "采购订单入库"})
    @RequestMapping("/inStore")
    public AjaxResult inStore(Orderdetail orderdetail, @SessionAttribute("emp") Emp emp) {
        return this.ordersService.inStore(orderdetail, emp);
    }

    @CheckPower({"采购申请", "销售订单出库"})
    @RequestMapping("/outStore")
    public AjaxResult ourStore(Orderdetail orderdetail, @SessionAttribute("emp") Emp emp) {
        return this.ordersService.outStore(orderdetail, emp);
    }

}
